package com.obas.business.view {
   import com.ease.framework.CommandCompletionEvent;
   import com.obas.constants.TestingLibConstants;
   import com.obas.event.DataRefreshEvent;
   import com.obas.event.ItestOperationEvent;
   import com.obas.flex.common.util.ResourceUtil;
   import com.obas.model.business.BusinessRepository;
   import com.obas.model.DataRefreshHelper;
   import com.ease.component.constant.LoadingStateName;
   import com.ease.component.managers.ErrorManager;

   import flash.events.EventDispatcher;
   import flash.events.IEventDispatcher;
   import flash.events.MouseEvent;

   import mx.collections.ArrayCollection;
   import mx.managers.PopUpManager;
   import mx.validators.Validator;

   [Event(name="{com.obas.event.ItestOperationEvent.REQUEST_ID}", type="com.obas.event.ItestOperationEvent")]
   [Event(name="{com.obas.event.DataRefreshEvent.REQUEST_ID}", type="com.obas.event.DataRefreshEvent")]
   public class BusinessRepositoryViewMediator extends EventDispatcher {

      public var _view:BusinessRepositoryView;

      [InjectableView]
      public function get view():BusinessRepositoryView {
         return _view;
      }

      public function set view(value:BusinessRepositoryView):void {
         _view = value;
         init();
      }

      private function init():void {
         _view.cancelButton.addEventListener(MouseEvent.CLICK, onCancel);
         _view.okButton.addEventListener(MouseEvent.CLICK, onOk);

         if(_view.actionType == TestingLibConstants.ACTION_UPDATE) {
            _view.title = "修改业务场景库";
         }
      }

      private function onCancel(e:* = null):void {
         PopUpManager.removePopUp(view);
      }

      private function onOk(e:*):void {

         var vrets:Array = Validator.validateAll(view.validators);

         if(vrets && vrets.length > 0) {
            return;
         }

         if(_view.actionType == TestingLibConstants.ACTION_CREATE) {
            var newBusinessRepository:BusinessRepository = new BusinessRepository();
            newBusinessRepository.name = view.businessRepositoryName.text;
            view.loadingState = LoadingStateName.LOADING;
            dispatchEvent(new ItestOperationEvent([newBusinessRepository], ItestOperationEvent.ADD_BUSINESSREPOSITORY));
         }
         else if(_view.actionType == TestingLibConstants.ACTION_UPDATE) {
            var businessRepository:BusinessRepository = _view.businessRepository;
            if(businessRepository.name == _view.businessRepositoryName.text) {
               return;
            }
            businessRepository.name = _view.businessRepositoryName.text;
            dispatchEvent(new ItestOperationEvent([businessRepository], ItestOperationEvent.UPDATE_BUSINESSREPOSITORY));
         }
      }

      [ResponseHandler("{com.obas.event.ItestOperationEvent.RETURN_ID}")]
      public function onTaskOperationEvent(event:CommandCompletionEvent):void {
         view.loadingState = LoadingStateName.LOADED;
         if (event.error) {
            ErrorManager.showErrorMessage(ResourceUtil.getErrorString("", event.error));
            return ;
         }
         var request:ItestOperationEvent = event.request as ItestOperationEvent;
         switch (request.actionType) {
            case ItestOperationEvent.ADD_BUSINESSREPOSITORY:
            case ItestOperationEvent.UPDATE_BUSINESSREPOSITORY:
               var businessRepository:BusinessRepository = event.result as BusinessRepository;
               var helper:DataRefreshHelper = new DataRefreshHelper();
               var lists:ArrayCollection = new ArrayCollection;
               lists.addItem(businessRepository);
               helper.data = lists;
               dispatchEvent(new DataRefreshEvent(helper, DataRefreshEvent.TYPE_ADD_UPDATE));
               onCancel();
               break;
         }
      }
   }
}